<template>
	<div>
		<el-form  :model="formPeople" :rules="rulesPeople" ref="rulePeople" label-width="150px" size="medium ">
			<el-col :xs="24" :sm="12" :md="8" :lg="8" :xl="8" v-for="item in peopleList" :key="item.prop" class="mb20">
				<el-form-item :label="item.label" :prop="item.prop">
					<el-input placeholder="请输入内容" v-model="formPeople[item.prop]" autocomplete="off" readonly @click.native="choosePersonnel(item.prop)">
						<template slot="append">选择用户</template>
					</el-input>
				</el-form-item>
			</el-col>
			<el-col :xs="24" :sm="12" :md="8" :lg="8" :xl="8" class="mb20">
				<el-form-item label="接受交底人" prop="dh_work_access_people">
					<el-input placeholder="请输入内容" style="width: 100%" v-model="formPeople[work_access_people]" autocomplete="off" disabled></el-input>
				</el-form-item>
			</el-col>

		</el-form>
		<el-drawer title="人员选择" :visible.sync="dialogFormVisiblePersonnel" :modal="false" size="50%" :with-header="false">
			<personnelSelection :multiple="true" @setCheckedList="getCheckedPersonnelList" :key="timerPersonnel" :isContractorLocal="isContractorLocal" :echoCheckedLabel="echoCheckedLabel">
			</personnelSelection>
		</el-drawer>
	</div>
</template>

<script>
import personnelSelection from '@/components/personnelSelection'
export default {
	components: {
		personnelSelection
	},
	props: {
		ticketType: String,
		peopleList: Array, // 接收表单数据对象   
		isContractor: {
			type: Boolean,
			default: false,
		}
	},
	data() {
		return {
            isContractorLocal: this.isContractor, // 将 props 的值赋值给 data 属性
			formPeople: {},
			dialogFormVisiblePersonnel: false,
			echoCheckedLabel: [],
			timerPersonnel: null,
			PersonnelIdStr: '',
			PersonnelNameStr: '',
			PersonnelType: '', //判断选择什么人员
			list: {},
			rulesPeople: {},
			work_access_people: ''
		}
	},
	methods: {
		// 人员选择点击事件
		choosePersonnel(type) {
			this.PersonnelType = type
			this.timerPersonnel = new Date().getTime()
			this.dialogFormVisiblePersonnel = true
			// console.log(this.PersonnelType)
			this.echoCheckedLabel = this.formPeople[type] ? this.formPeople[type].split(",") : [];
			// console.log(this.formPeople[type])
			// console.log(this.echoCheckedLabel)
		},
		// 人员列表确定 获取人员选择数据
		getCheckedPersonnelList(list) {
			let arr = list.map((i) => { return { name: i.name, id: i.id, type: i.type } })
			this.list[this.PersonnelType] = arr
			// console.log(this.list)
			this.PersonnelIdStr = list.map((i) => i.id).join(',')
			this.PersonnelNameStr = list.map((i) => i.name).join(',')
			this.personConfirm() // 人员赋值事件
		},
		personConfirm() {
			// const personnelTypeMap = {
			//     'dh_risk_safe': ['dh_risk_safe', 'dh_risk_safe_id'],
			//     'dh_work_people': ['dh_work_people', 'dh_work_people_id'],
			//     'ticket_people': ['ticket_people', 'ticket_people_id'],
			//     'dh_work_tell_people': ['dh_work_tell_people', 'dh_work_tell_people_id'],
			//     'gas_people': ['gas_people', 'gas_people_id'],
			//     'related_people': ['related_people', 'related_people_id'],
			//     'dh_work_dute_people': ['dh_work_dute_people', 'dh_work_dute_people_id'],
			//     'dh_work_dute_people_p': ['dh_work_dute_people_p', 'dh_work_dute_people_p_id'],
			//     'fire_people': ['fire_people', 'fire_people_id'],
			//     'dh_work_accep': ['dh_work_accep', 'dh_work_accep_id'],
			//     'dh_work_accep_p': ['dh_work_accep_p', 'dh_work_accep_p_id'],
			//     'dh_work_take_people': ['dh_work_take_people', 'dh_work_take_people_id'],
			// }
			// 生成personnelTypeMap对象
			const personnelTypeMap = {};
			this.peopleList.forEach(item => {
				personnelTypeMap[item.prop] = [item.prop, `${item.prop}_id`]
			})
			const [nameKey, idKey] = personnelTypeMap[this.PersonnelType]
			this.formPeople[nameKey] = this.PersonnelNameStr
			this.formPeople[idKey] = this.PersonnelIdStr
			this.dialogFormVisiblePersonnel = false

			// 处理 work_access_people 字段
			this.formPeople[this.work_access_people] = [
				this.formPeople.work_take_people,
				this.formPeople.work_dute_people,
				this.formPeople.work_dute_people_p,
			].filter(item => item).join(',');

			// 处理 work_access_people_id 字段
			this.formPeople[this.work_access_people_id] = [
				this.formPeople.work_take_people_id,
				this.formPeople.work_dute_people_id,
				this.formPeople.work_dute_people_p_id,
			].filter(item => item).join(',');

			// console.log(this.formPeople)
		},
		confirm() {
			// return new Promise((resolve, reject) => {
			//     this.$refs['rulePeople'].validate((valid) => {
			//     if (valid) {
			//         resolve('Yes'); // 验证通过时，将'Yes'传递给resolve
			//     } else {
			//         reject('No'); // 验证未通过时，将'No'传递给reject
			//     }
			//     });
			// });
			return new Promise((resolve, reject) => {
				if (this.$refs['rulePeople'] && this.$refs['rulePeople'].validate) {
					this.$refs['rulePeople'].validate((valid) => {
						if (valid) {
							resolve([this.formPeople, this.list]);
						} else {
							resolve('No');
						}
					});
				} else {
					reject("rulePeople reference or validate method not found!");
				}
			});
		},
		updateContractorData(status) {
			this.isContractorLocal = status
		},
		initPeopleData() {
            this.formPeople = {
				"work_take_people": "lxx,admin",
				"work_tell_people": "林帅",
				"work_dute_people": "admin",
				"work_dute_people_p": "admin",
				"work_accep": "admin",
				"work_accep_p": "admin",
				"gas_people": "admin",
				"related_people": "admin",
				"ticket_people": "admin",
				"work_people": "admin",
				"risk_safe": "admin",
				"work_take_people_id": "67b92626db1447ba926f46e9e85f6427,e0842614d057494fa36cdf68ae574d47",
				"work_access_people": "lxx,admin,admin,admin",
				"work_access_people_id": "67b92626db1447ba926f46e9e85f6427,e0842614d057494fa36cdf68ae574d47,e0842614d057494fa36cdf68ae574d47,e0842614d057494fa36cdf68ae574d47",
				"work_tell_people_id": "564de7c0e0e8407a84997edcf1d7d0d7",
				"work_dute_people_id": "e0842614d057494fa36cdf68ae574d47",
				"work_dute_people_p_id": "e0842614d057494fa36cdf68ae574d47",
				"work_accep_id": "e0842614d057494fa36cdf68ae574d47",
				"work_accep_p_id": "e0842614d057494fa36cdf68ae574d47",
				"gas_people_id": "e0842614d057494fa36cdf68ae574d47",
				"related_people_id": "e0842614d057494fa36cdf68ae574d47",
				"ticket_people_id": "e0842614d057494fa36cdf68ae574d47",
				"work_people_id": "e0842614d057494fa36cdf68ae574d47",
				"risk_safe_id": "e0842614d057494fa36cdf68ae574d47",
            }
            this.list = {
				"work_take_people": [
				{"name": "lxx", "id": "67b92626db1447ba926f46e9e85f6427", "type": 0},
				{"name": "admin", "id": "e0842614d057494fa36cdf68ae574d47", "type": 0}
				],
				"work_tell_people": [{"name": "林帅", "id": "564de7c0e0e8407a84997edcf1d7d0d7", "type": 0}],
				"work_dute_people": [{"name": "admin", "id": "e0842614d057494fa36cdf68ae574d47", "type": 0}],
				"work_dute_people_p": [{"name": "admin", "id": "e0842614d057494fa36cdf68ae574d47", "type": 0}],
				"work_accep": [{"name": "admin", "id": "e0842614d057494fa36cdf68ae574d47", "type": 0}],
				"work_accep_p": [{"name": "admin", "id": "e0842614d057494fa36cdf68ae574d47", "type": 0}],
				"gas_people": [{"name": "admin", "id": "e0842614d057494fa36cdf68ae574d47", "type": 0}],
				"related_people": [{"name": "admin", "id": "e0842614d057494fa36cdf68ae574d47", "type": 0}],
				"ticket_people": [{"name": "admin", "id": "e0842614d057494fa36cdf68ae574d47", "type": 0}],
				"work_people": [{"name": "admin", "id": "e0842614d057494fa36cdf68ae574d47", "type": 0}],
				"risk_safe": [{"name": "admin", "id": "e0842614d057494fa36cdf68ae574d47", "type": 0}]
			}
		},
	},
	mounted() {
		// console.log(this.peopleList)
		this.peopleList.forEach(item => {
			this.$set(this.formPeople, item.prop, ''); // 初始化form对象的属性
			this.$set(this.rulesPeople, item.prop, [{ required: true, message: '该字段为必填项', trigger: 'change' }]);
		});

	},
	created() {
		this.work_access_people = 'work_access_people'
		this.work_access_people_id = 'work_access_people_id'

	}
}
</script>

<style></style>